package com.clwl.supervise.superviseapp.controller.investigation;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clwl.supervise.superviseapp.entity.base.CommonSearchEntity;
import com.clwl.supervise.superviseapp.entity.base.SysDictData;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.supervise.BangbanWenshuInfo;
import com.clwl.supervise.superviseapp.entity.supervise.JgDocTemplate;
import com.clwl.supervise.superviseapp.entity.supervise.JgInvestigation;
import com.clwl.supervise.superviseapp.service.investigation.InvestigationService;
import com.clwl.supervise.superviseapp.service.investigation.JgDsjXzcfDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 调查处理
 */
@RestController
@RequestMapping("investigation")
public class InvestigationController {

    @Resource
    private InvestigationService investigationService;
    @Autowired
    private JgDsjXzcfDocService jgDsjXzcfDocService;

    /**
     * 获取调查处理列表信息
     *
     * @param pageNum                   当前页数
     * @param pageSize                  每页数据数量
     * @return
     */
    @GetMapping("page")
    public ResultEntity<List<Map<String, Object>>> page(@RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize, CommonSearchEntity commonSearchEntity ) {
        try {
            IPage<Map<String, Object>> pageList = investigationService.selectInvestigationPage(pageNum, pageSize, commonSearchEntity);
            return ResultEntity.success(pageList.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取列表信息失败");
        }
    }

    /**
     * 获取调查处理中的问题项
     *
     * @param investigationId 调查处理ID
     * @return
     */
    @GetMapping("getProblemList")
    public ResultEntity<List<Map<String, Object>>> getProblemList(@RequestParam(required = true) Long investigationId) {
        try {
            List<Map<String, Object>> pageList = investigationService.getProblemList(investigationId);
            return ResultEntity.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取列表信息失败");
        }
    }

    /**
     * 获取调查处理中处选择不立案的时候不予立案的理由选项
     *
     * @return
     */
    @GetMapping("notFilingList")
    public ResultEntity<List<SysDictData>> notFilingList() {
        try {
            List<SysDictData> pageList = investigationService.notFilingList();
            return ResultEntity.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取列表信息失败");
        }
    }


    /**
     * 上传立案审批表文件
     *
     * @param investigationId 调查处理ID
     * @param file            文件
     * @param type            文件类型：1-立案审批表；2-决定书；3-不予立案审批表;4-不予立案的情况说明文件
     * @return
     */
    @PostMapping("uploadInvestigationFile")
    public ResultEntity<Map<String,Object>> uploadInvestigationFile(@RequestParam(required = true) Long investigationId, @RequestPart(value = "file", required = true) MultipartFile file, @RequestParam(required = true) String type) {
        try {
            if (file == null || file.getOriginalFilename().length() == 0) {
                return ResultEntity.error("请上传文件信息");
            }
            Map<String,Object> filePath = investigationService.uploadInvestigationFile(investigationId, file, type);
            return ResultEntity.success(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }


    /**
     * 保存调查处理信息
     *
     * @param investigationId
     * @param filing          是否立案；1-立案。2-不立案
     * @param temporary       是否临时保存；1-临时。2-不临时
     * @param noFilingReason  不予立案的理由
     * @return
     */
    @PostMapping("saveInvestigationInfo")
    public ResultEntity<List<Map<String, Object>>> saveInvestigationInfo(@RequestParam(required = true) Long investigationId, @RequestParam(required = true) String filing, @RequestParam(required = true) String temporary, String noFilingReason) {
        try {
            investigationService.saveInvestigationInfo(investigationId, filing, temporary, noFilingReason);
            return ResultEntity.success();
        } catch (Exception e) {
            return ResultEntity.error(e.getMessage());
        }
    }


    /**
     * 查询已经处理完成的调查处理信息
     *
     * @param investigationId 调查处理id
     * @return
     */
    @GetMapping("getInvestigationInfo")
    public ResultEntity<JgInvestigation> getInvestigationInfo(@RequestParam(required = true) Long investigationId) {
        try {
            JgInvestigation investigation = investigationService.getInvestigationInfo(investigationId);
            return ResultEntity.success(investigation);
        } catch (Exception e) {
            return ResultEntity.error(e.getMessage());
        }
    }

    /**
     * 获取文书模板列表
     *
     * @param templateType 模板列表的类型;1-立案审批模板；2-决定书模板；3-不予立案审批模板
     * @return
     */
    @GetMapping("getTemplateList")
    public ResultEntity<List<JgDocTemplate>> getTemplateList(@RequestParam(required = false) String templateType) {
        try {
            List<JgDocTemplate> templateList = investigationService.getTemplateList(templateType);
            return ResultEntity.success(templateList);
        } catch (Exception e) {
            return ResultEntity.error(e.getMessage());
        }
    }

    /**
     * 获取文书模板需要填写的内容
     *
     * @param templateId      模板ID
     * @param investigationId 调查处理ID
     * @return
     */
    @GetMapping("getTemplateInputList")
    public ResultEntity<List<Map<String, Object>>> getTemplateInputList(@RequestParam(required = true) Long templateId, Long investigationId) {
        try {
            List<Map<String, Object>> templateInputList = investigationService.getTemplateInputList(templateId, investigationId);
            return ResultEntity.success(templateInputList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }

    /**
     * 生成模板内容
     * 以form方式传递getTemplateInputList接口中获取的的code=填写的值
     *
     * @param templateId 模板id
     * @return
     */
    @PostMapping("generatorDocument")
    public ResultEntity<String> generatorDocument(Long templateId, HttpServletRequest request) {
        try {
            Enumeration<String> parameterNames = request.getParameterNames();
            Map<String, Object> parameterMap = new HashMap<>();
            while (parameterNames.hasMoreElements()) {
                String key = parameterNames.nextElement();
                parameterMap.put(key, request.getParameter(key));
            }
            String templateText = investigationService.generatorDocument(templateId, parameterMap);
            return ResultEntity.success(templateText);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }


    /**
     * 生成模板字段帮办
     *
     * @param code            需要帮办的编码
     * @param investigationId 调查处理id
     * @param templateId      模板id
     * @return
     */
    @GetMapping("fieldHelp")
    public ResultEntity<BangbanWenshuInfo> fieldHelp(String code, Long investigationId, Long templateId) {
        try {
            BangbanWenshuInfo bangbanWenshuInfo = investigationService.getFieldHelp(code, investigationId, templateId);
            return ResultEntity.success(bangbanWenshuInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }

    @GetMapping("getPunishmentCases")
    public ResultEntity<Map<String,Object>> getPunishmentCases(String content) {
        try {
            Map<String,Object> resultMap=jgDsjXzcfDocService.getJgDsjXzcfDocByInputStr(content);
            if("0".equals(resultMap.get("code"))){
                Map<String,Object> dataMap=(Map)resultMap.get("data");
                return ResultEntity.success(dataMap);
            }else{
                ResultEntity.error((String)resultMap.get("msg"));
            }

        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
       return ResultEntity.error("获取执法案例失败");
    }
    /**
     * 文书帮办查询
     *
     * @param investigationId 调查处理id
     * @param templateId      模板id
     * @return
     */
    @GetMapping("documentHelp")
    public ResultEntity<Map<String, Object>> documentHelp(Long investigationId, Long templateId) {
        try {
            Map<String, Object> bangbanWenshuMap = investigationService.documentHelp(investigationId, templateId);
            
            return ResultEntity.success(bangbanWenshuMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }


}
